package com.chat.controller;

import com.chat.response.BaseResponse;
import com.chat.response.message.BaseMessageResponse;
import com.chat.service.AuthenticationService;
import com.chat.service.PushService;
import com.chat.utils.RedisMessageUtil;
import com.chat.utils.RedisUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.Pattern;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeoutException;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Validated
@RestController
@RequestMapping("/cat/auth")
public class AuthenticationController {
    final
    AuthenticationService authenticationService;
    private final String NUMBER = "^\\d{6}$";
    private final String USER_NAME = "^[a-zA-Z0-9一-龥]+$";
    private final String    EMAIL_REGEX = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
    @Resource
    private       RedisUtil redisCache;

    @Resource
    RedisMessageUtil redisMessageUtil;
    @Resource
    PushService                               pushService;

    public AuthenticationController(AuthenticationService authenticationService) {
        this.authenticationService = authenticationService;
    }

    @GetMapping("/message/{id}")
    public BaseResponse<List<BaseMessageResponse>> message(@PathVariable String id) throws IOException, TimeoutException, InterruptedException {
        List<BaseMessageResponse> receiveMessagesVos = redisMessageUtil.getAndClearOfflineMessages(Long.valueOf(id));
        return BaseResponse.success(receiveMessagesVos);
    }

    @PostMapping("/del-redis")
    public BaseResponse<String> delRedisUser(Long id) {
        redisCache.deleteObject("login:" + id);
        return BaseResponse.success("要再回来哦！");
    }

    @PostMapping("/valid-register-email")
    public BaseResponse<String> validateEmail(@Pattern(regexp = EMAIL_REGEX) @RequestParam("email") String email, HttpSession session) {
        authenticationService.sendValidateEmail(email, session.getId(), false);
        return BaseResponse.success("邮件已发送，请稍后");
    }

    @PostMapping("/valid-reset-email")
    public BaseResponse<String> validateResetEmail(@Pattern(regexp = EMAIL_REGEX) @RequestParam("email") String email,
                                               HttpSession session) {
        authenticationService.sendValidateEmail(email, session.getId(), "updatePassword");
        return BaseResponse.success("邮件已发送，请稍后");
    }

    @PostMapping("/register")
    public BaseResponse<String> registerUser(
            @Pattern(regexp = USER_NAME) @Length(min = 2, max = 8) @RequestParam("username") String username,
                                         @Length(min = 6, max = 20) @RequestParam("password") String password,
                                         @Pattern(regexp = EMAIL_REGEX) @RequestParam("email") String email,
                                         @Length(min = 6, max = 6) @Pattern(regexp = NUMBER) @RequestParam("code") String code,
                                         HttpSession session
    ) {
        authenticationService.validateAndRegisterUser(username, password, email, code,
                session.getId());
        return BaseResponse.success("注册成功");

    }

    @PostMapping("/do-reset")
    public BaseResponse<String> resetPassword(@Length(min = 6, max = 16) @RequestParam("password") String password,
                                          HttpSession session) {
        authenticationService.resetPassword(password);
        return BaseResponse.success("密码重置成功");
    }

    @PostMapping("/start-reset")
    public BaseResponse<String> startReset(@Pattern(regexp = EMAIL_REGEX) @RequestParam("email") String email,
            @Length(min = 6, max = 6) @RequestParam("code") String code) {
        authenticationService.validateOnly(email, code, "updatePassword");
        return BaseResponse.success();

    }
}
